#!/usr/bin/perl -w
use warnings;
use strict;

use constant (
    ADD_COORD => 1,
);

my $cl_pf_0 = [ qw(  3  4 ) ];
my $cl_pf_1 = [ qw(  7  4 ) ];
my $cl_pf_2 = [ qw( 15  4 ) ];

my $cl_r3_0 = [ qw(  8 27 46 ) ];
my $cl_r4_0 = [ qw(  5  7 27 46 ) ];
my $cl_r5_0 = [ qw(  5  7 27 45 46 ) ];

my $cond_1 = sub {
    my ($ic) = @_;
    return $ic->{VGHC_display} >= 12 && $ic->{VGLC_display} <= -5;
};

my $cond_tma3c = sub {
    my ($ic) = @_;
    return 1
        # reset current
        && $ic->{Idd_reset}     <= 0.1          # mA
        # display voltages
        && 1.4 <= $ic->{VDD18C_display}
        &&        $ic->{VDD18C_display} <= 2.3
        && $ic->{VCOMH_display} >= 3.1
        && $ic->{VCOML_display} <= -0.9
        && $ic->{VLCDC_display} >= 4.5
        && $ic->{VGHC_display}  >= 12
        && $ic->{VGLC_display}  <= -5
        && $ic->{Idd_display}   <= 10
        # RAM/register
        && $ic->{Register_read} eq 'P'
        && $ic->{RAM_cb}        eq 'P'
        && $ic->{RAM_one}       eq 'P'
        && $ic->{RAM_zero}      eq 'P'
        && $ic->{RAM_gs}        eq 'P'
        # dispay timing;
        && $ic->{DisplayTiming} eq 'P';
};

my $cond_tma4c_inc = sub {
    my ($ic) = @_;
    return 1
        # reset current
        && $ic->{Idd_reset}     <= 0.6          # mA
        # display voltages
        #&& 1.4 <= $ic->{VDD18C_display}
        #&&        $ic->{VDD18C_display} <= 2.3
        && $ic->{VCOMH_display} >= 3.1
        && $ic->{VCOML_display} <= -0.5
        && $ic->{VLCDC_display} >= 4.5
        && $ic->{VGHC_display}  >= 12
        && $ic->{VGLC_display}  <= -5
        && $ic->{Idd_display}   <= 10
        # RAM/register
        && $ic->{Register_read} eq 'P'
        && $ic->{RAM_cb}        eq 'P'
        && $ic->{RAM_one}       eq 'P'
        && $ic->{RAM_zero}      eq 'P'
        && $ic->{RAM_gs}        eq 'P'
        # dispay timing;
        && $ic->{DisplayTiming} eq 'P';
};

my $coord_item = sub {
    my ($ic, $adr_tbl) = @_;
    my ($xadr, $yadr) = ($ic->{Xadr}, $ic->{Yadr});
    my $ic_nos = $adr_tbl->{$xadr}{$yadr};
    return join('/', @$ic_nos);
};

my $sort_sub = sub {
    my ($ic) = @_;
    if ($cond_tma3c->($ic)) {
        return 'cat-1';
    } elsif ($cond_tma4c_inc->($ic)) {
        return 'cat-2';
    } else {
        return 'FAIL';
    }
};

my $repo = [
    +{ item => 'Idd_reset', worksheet => 'Reset-Current', compare => 0,
        colors => [ 10, 44, 38, 8, 5 ], ranges => [ 0.1, 0.3, 0.6, 1 ],
    },
    +{ item => 'PF', worksheet => 'Pass_Fail', compare => 4,
       colors => [ 4, 3, 1], ranges => [ qw( PASS FAIL ) ],
    },
    +{ item => $sort_sub, worksheet => 'Sort', compare => 4,
       colors => [ 4, 5, 15 ], ranges => [ qw( cat-1 cat-2 FAIL ) ],
    },
    +{ item => $coord_item, worksheet => 'Coordinate', compare => 0, options => ADD_COORD,
        #colors => $cl_r5_0, ranges => [ 0.1, 0.3, 0.6, 1 ],
    },
];

$repo;
